
function report(uid, val){
    $.post(base_url+"source/ajax.php?action=report", {
        uid: uid,
        order: val
    });
}
function is_numeric(input){
    return !isNaN(input);
}

function exit(){
    $.post(base_url+"source/ajax.php?action=exit", {
        exit: 1
    }, function(data){
        window.location.href = "index.php";
    });
    
}

function update_table(){
    setTimeout(update_table,5000);
    var $date = $('.calendar:first');
    $.post(base_url+"source/ajax.php?action=get_disabled", {
        date: $date.attr("id")
    }, function(data){
        data = jQuery.parseJSON(data);
        for(key in data){
            daydata = data[key];
            if(daydata == 1){
                $('#'+key).addClass('calendar_disabled');
                $('#'+key).removeClass('calendar_enabled');
                $('.date_'+key).addClass('disabled_order');
                $('.date_'+key).removeClass('order');
                $('.menu_'+key).remove();
            } 
            else {
                if(daydata){
                    daydata = jQuery.parseJSON(daydata);
                    for(k in daydata.keys){
                        $('#'+daydata.keys[k]).html(daydata[daydata.keys[k]]);
                    }
                    $('#total_fc_'+key).html(daydata['0.4']);
                    $('#total_mc_'+key).html(daydata['0.5']);
                    $('#'+key).addClass('calendar_enabled');
                    $('#'+key).removeClass('calendar_disabled');
                    $('.date_'+key).addClass('order');
                    $('.date_'+key).removeClass('disabled_order');
                } else {
                    $('#'+key).addClass('calendar_enabled');
                    $('#'+key).removeClass('calendar_disabled');
                    $('.date_'+key).addClass('order');
                    $('.date_'+key).removeClass('disabled_order');
                }
            }
        }
        $('.order:not(".me")').addClass('disabled_order'); 
        $('.order:not(".me")').removeClass('order');
    });
}

$(function(){
    var order_option = {
        "":"0",
        "0":"0",
        "0.4":"П",
        "0.5":"В",
        "1":"Ц"
    };
    $('.enter_button').click(function(){
        $('.enter_button').animate({
            opacity: 0
        }, 300, function() {
            $('.enter_button').css("display", "none");
            $('.enter_form').show();
        });
    });
    
    $('.order').live("click", function(){
        var menu = $(this);
        $('.popup').remove();
        var top = $(this).offset().top-45;
        var left = $(this).offset().left-45;
        $.post(base_url+"source/ajax.php?action=show_menu", {
            date_uid: menu.attr('id'),
            top: top,
            left: left
        }, function(data){
            menu.after(data);
        });
    });
    $('.menubutton').live("click",function(){
        $(this).closest('.order_active').prev('.order').html(order_option[$(this).attr('property')]);
        report($(this).closest('.order_active').prev('.order').attr('id'), $(this).attr('property'));
    });
    $('.order_active').live("click",function(){
        $(this).remove();
    });
    $('#showpass').click(function(){
        if($(this).attr("checked")=="checked"){
            $('#password_hidden').hide();
            $('#password_open').show();
        } else{
            $('#password_hidden').show();
            $('#password_open').hide();
        }
    });
    $('.pass_val').change(function(){
        $('.pass_val').val($(this).val());
    });
    $('#exit_button').click(function(){
        exit();
    });
    $('#close_addname').click(function(){
        $("#addname").remove();
    });
    update_table();
    $("#login_form").submit(function(){
        $('.pass_val').val($('.pass_val:visible').val());
    });
});